<?php
//image module

class image_module {
	
	public function page($page, $number)
	{
		$start = (($page - 1) * $number);
		
		$query = new Query("SELECT * FROM `images` ORDER BY `date` DESC LIMIT ?, ?", $start, $number);
		
		if($query->num_rows() > 0)
		{
			$images = image::RecordSet($query, 'image');
			
			#display these images
			$show .= self::display($images);
		}
		else
		{
			$show = "There are currently no images.";
		}
		
		return $show;
	}
	
	//display a picture
	public function displaySingle($id)
	{
		$image = new image($id);
		
		if($image->title != '')
		{
			$show = '<h2>' . $image->title .'</h2>';
		}
		
		$show .= '<p><a href="' . ROOT_URL . 'content/images/' . $image->url .'"><img src="' . ROOT_URL . 'content/images/med/' . $image->url . '" alt="' . $image->title . '"/></a></p>
				  <p>' . $image->caption . '</p>';
		
		return $show;
		
	}
	
	public function display($imgs)
	{
		foreach($imgs as $img)
		{
			$show .= '<p>' . display::link('<img src="'. ROOT_URL . 'content/images/small/' . $img->url . '" alt="' . $img->title . '" />', 'image/' . $img->id) . '</p>';
		}
		
		return $show;
	}
	
	public function archive($year)
	{
		$query = new Query("SELECT * FROM `images` WHERE `date` >= ? and `date` <= ? ORDER BY `date` DESC", mktime(0, 0, 0, 1, 1, $year), mktime(0, 0, 0, 12, 31, $year));
		if($query->num_rows() > 0)
		{
		
			$imgs = image::recordSet($query, 'image');
				
			return self::display($imgs);
		}
		else
		{
			return 'No Images Entries Found';
		}
	}
		
	//get the years that have previous entries
	public function archiveYears()
	{
		$year = date('Y', time());
		
		$hasNextYear = true;
		
		while($hasNextYear)
		{
			$query = new Query("SELECT * FROM `images` WHERE `date` < ? AND `date` >= ? ORDER BY `id`", mktime(0,0,0,1,1,$year), mktime(0,0,0,1,1,$year - 1));
			if($query->num_rows() > 0)
			{
				$show .= display::headerLink($year - 1, 'image/archive/' . ($year - 1));
			}
			
			$query = new Query("SELECT * FROM `images` WHERE `date` < ? ORDER BY `id`", mktime(0,0,0,1,1,$year - 1));
			if($query->num_rows()>0)
			{
				$year--;
			}
			else
			{
				$hasNextYear = false;
			}
			
		}
		
		return $show;
	}
	
	public function archiveImages()
	{
		$query = new Query("SELECT * FROM `images` WHERE `date` >= ? ORDER BY `date` DESC LIMIT 0, 3", mktime(0,0,0,1,1,date('Y',time())));
		
		if($query->num_rows() > 0)
		{
			$imgs = image::RecordSet($query, 'image');
			
			#display these news entries
			$show .= self::display($imgs);
		}
		else
		{
			$show = "There are currently no images for the current year.";
		}
		
		return $show;
	}

}
?>